package com.zhangtai.modules.service.impl.contract;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhangtai.modules.dao.ContractRecordDao;
import com.zhangtai.modules.dto.ContractRecordDto;
import com.zhangtai.modules.entity.contract.ContractRecordEntity;
import com.zhangtai.modules.service.contract.ContractRecordService;
import com.zhangtai.utils.PageResult;
import com.zhangtai.utils.R;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;


@RestController
public class ContractRecordServiceImpl extends ServiceImpl<ContractRecordDao, ContractRecordEntity> implements ContractRecordService {

    @Autowired
    private ContractRecordDao contractRecordDao;

    @Override
    public PageResult queryPage(@RequestParam(value = "keyword", required = false) String keyword,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        IPage<ContractRecordEntity> page = this.page(
                new Page<ContractRecordEntity>(pageNum,pageSize),
                new QueryWrapper<ContractRecordEntity>().like(!StringUtils.isEmpty(keyword),"name",keyword)
                        .eq("is_delete",0)
        );

        return new PageResult(page);
    }

    @Override
    public PageResult<ContractRecordDto> getList(@RequestParam(value = "code", required = false) String code,
                                                 @RequestParam(value = "name", required = false) String name,
                                                 @RequestParam(value = "orgId", required = false) List<Long> orgId,
                                                 @RequestParam(value = "unitId", required = false) List<Long> unitId,
                                                 @RequestParam(value = "contractType", required = false) Integer contractType,
                                                 @RequestParam(value = "contractTimeType", required = false) Integer contractTimeType,
                                                 @RequestParam(value = "status", required = false) Integer status,
                                                 @RequestParam(value = "contractEndDate", required = false)String contractEndDate,
                                                 @RequestParam(value = "orgIds", required = false) List<Long> orgIds,
                                                 @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                 @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
        IPage<ContractRecordDto> page = new Page<ContractRecordDto>(pageNum,pageSize);
        List<ContractRecordDto> list = contractRecordDao.getRecordList(code,name,orgId,unitId,contractType,contractTimeType,status,contractEndDate,orgIds,page);
        page.setRecords(list);
        return new PageResult<ContractRecordDto>(page);
    }

    @Override
    public ContractRecordEntity getByIdE(Long id) {
            ContractRecordEntity one = this.getOne(new QueryWrapper<ContractRecordEntity>().eq("id", id).eq("is_delete", 0));

        return one;
    }

    public void removeRecord(Long oid,Integer kind){
        contractRecordDao.removeRecord(oid,kind);
    }

    @Override
    public R saveE(ContractRecordEntity entity) {
        boolean b = this.save(entity);
        if (!b){
            return R.error();
        }
        return R.ok();

    }

    @Override
    public R updateByIdE(ContractRecordEntity entity) {
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    public R removeOneById(Long id) {
            ContractRecordEntity entity = new ContractRecordEntity();
        entity.setId(id);
        entity.setIsDelete(1);
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    @Transactional
    public R removeByIdsE(List<Long> ids) {
        for (Long id : ids) {
            R r = removeOneById(id);
            if (!r.getFlag()){
                return r;
            }
        }
        return R.ok();
    }

}